package com.object.module.sys.service.impl;

import com.object.configuration.utils.Q;
import com.object.module.chatroom.entity.TAddRecordEntity;
import com.object.module.chatroom.entity.TGoodFriendEntity;
import com.object.module.chatroom.service.TAddRecordService;
import com.object.module.chatroom.service.TGoodFriendService;
import com.object.module.sys.ov.TGoodFriendOv;
import com.object.module.sys.ov.TUserAndAddUser;
import com.object.module.sys.service.TRoleService;
import com.object.configuration.utils.PasswordEncryp;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.hibernate.validator.constraints.br.TituloEleitoral;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.configuration.utils.PageUtils;
import com.object.configuration.utils.Query;

import com.object.dao.sys.TUserDao;
import com.object.module.sys.entity.TUserEntity;
import com.object.module.sys.service.TUserService;
import org.springframework.transaction.annotation.Transactional;


@Service("TUserServiceImpl")
public class TUserServiceImpl extends ServiceImpl<TUserDao, TUserEntity> implements TUserService {

    @Autowired
    private TRoleService roleServicel;
    @Autowired
    private TAddRecordService addRecordService;
    @Autowired
    private TUserDao usserdao;
    @Autowired
    private TGoodFriendService goodFriendService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TUserEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("ur_username", key);
        }
        IPage<TUserEntity> page = this.page(
                new Query<TUserEntity>().getPage(params),
                wrapper
        );
        for (TUserEntity user : page.getRecords()) {
            user.setRole(roleServicel.getById(user.getUrReId()));
        }

        return new PageUtils(page);
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public TUserEntity findLogin(TUserEntity user) {
        QueryWrapper<TUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("ur_username", user.getUrUsername());
        TUserEntity one = getOne(wrapper);
        if (PasswordEncryp.IfPassowrd(user.getUrPassword(), one.getUrPassword())) {
            //密码正确
            return one;
        }
        return null;
    }

    /**
     * 密码修改
     *
     * @param oldpassword
     * @param newpassword
     * @param userId
     * @return
     */
    @Override
    public boolean updatePassword(String oldpassword, String newpassword, String userId) {
        TUserEntity userEntity = getById(userId);
        if (PasswordEncryp.IfPassowrd(oldpassword, userEntity.getUrPassword())) {
            //输入的旧密码与数据库的密码一样      把新输入的密码加密并且存储到数据库中去
            userEntity.setUrPassword(PasswordEncryp.getPaawordEncryp(newpassword));
            //修改密码
            boolean b = updateById(userEntity);
            return b;
        }
        return false;
    }

    @Override
    public TUserEntity findById(Integer urId) {
        TUserEntity user = getById(urId);
        user.setRole(roleServicel.getById(user.getUrReId()));
        return user;
    }

    /**
     * 查找用户
     *
     * @param urUsername
     * @return
     */
    @Override
    public TUserEntity infoUrUsername(String urUsername) {
        QueryWrapper<TUserEntity> wrapper = new QueryWrapper<TUserEntity>().eq("ur_username", urUsername);
        TUserEntity userEntity = getOne(wrapper);
        return userEntity;
    }

    @Override
    @Transactional
    public String addUser(Integer userId, Integer addUserId) {
         QueryWrapper<TAddRecordEntity> wrapper=new QueryWrapper<TAddRecordEntity>()
                 .eq("r_u_in",userId).eq("r_u_out",addUserId);
        TAddRecordEntity addRecordEntity1 = addRecordService.getOne(wrapper);
        if (addRecordEntity1!=null) {
             return "请不要频繁添加,等待同意！";
        }
        TAddRecordEntity addRecordEntity = new TAddRecordEntity();
        addRecordEntity.setRUIn(userId);
        addRecordEntity.setRUOut(addUserId);
        addRecordEntity.setRUTimeIn(new Date());
        val urName = findById(userId).getUrName();
        addRecordEntity.setRURemarks("我是" + urName);
        boolean flag = addRecordService.save(addRecordEntity);
        return flag?"ok":"好友申请失败！！";
    }

    /**
     * 获取好友的申请
     *Z
     * @param userId
     * @return
     */
    @Override
    public List<TUserAndAddUser> getAddUsers(Integer userId) {
        List<TUserAndAddUser> list = usserdao.getAddUsers(userId);
        return list;
    }

    /**
     *  同意好友
     * @param userId
     * @param agreeUserId
     * @return
     */
    @Override
    @Transactional
    public Q agreeUserId(Integer userId, Integer agreeUserId) {
        //修改同意时间
        boolean flag=false;
        QueryWrapper<TAddRecordEntity> wrapper=new QueryWrapper<TAddRecordEntity>().eq("r_u_in",agreeUserId)
                .eq("r_u_out",userId);
        TAddRecordEntity addRecordEntity=new TAddRecordEntity();
        addRecordEntity.setRUTimeOut(new Date());
        addRecordEntity.setRUIn(agreeUserId);
        addRecordEntity.setRUOut(userId);
        flag=addRecordService.update(addRecordEntity, wrapper);

        //保存到好友列表去
        TGoodFriendEntity goodFriendEntity=new TGoodFriendEntity();
        goodFriendEntity.setTOwn(userId);
        goodFriendEntity.setTGoodFriend(agreeUserId);
        val userEntity = getById(agreeUserId);
        goodFriendEntity.setTName(userEntity.getUrName());
        flag=goodFriendService.save(goodFriendEntity);
        //保存两个好友
        goodFriendEntity.setTOwn(agreeUserId);
        goodFriendEntity.setTGoodFriend(userId);
        flag=goodFriendService.save(goodFriendEntity);
        return flag?Q.ok():Q.error().put("msg","同意出现异常！！");
    }


    /**
     *  获取好友列表
     * @param userId
     * @return
     */
    @Override
    public Q getFriends(Integer userId) {
       List<TGoodFriendOv> list= usserdao.getFriends(userId);
        return Q.ok().put("data",list);
    }


}